package com.tarim.wheat.controller;

import com.tarim.wheat.model.File;
import com.tarim.wheat.service.FileService;
import com.tarim.wheat.util.Constants;
import com.tarim.wheat.util.JsonResult;
import com.tarim.wheat.util.UploadFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/file")
public class FileController {
    @Autowired
    FileService fileService;

    /**
     * 获取轮播图
     * @return
     */
    @RequestMapping(value = "/getCarousel")
    @ResponseBody
    public JsonResult getCarousel(){
        JsonResult js;
        try {
            List list = fileService.selectByName("轮播");
            js = new JsonResult(Constants.STATUS_SUCCESS, "获取轮播图成功", list);
        }catch (Exception ex){
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "获取异常");
        }
        return js;
    }

    /**
     * 单文件上传
     * @param file
     * @return
     */
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult uploadFile(MultipartFile file){
        JsonResult js;
        try {
            Map map = UploadFileUtil.upload(file);
            if(map == null){
                js = new JsonResult(Constants.STATUS_FAIL, "上传失败");
            }else {
                String fileName = map.get("fileName").toString();
                String fileUrl = map.get("fileUrl").toString();
                String fileSize = map.get("fileSize").toString();
                File insertFile = new File();
                insertFile.setFileName(fileName);
                insertFile.setFileUrl(fileUrl);
                insertFile.setFileSize(fileSize);
                int i = fileService.insert(insertFile);
                if(i == 0){
                    js = new JsonResult(Constants.STATUS_FAIL, "上传失败");
                    return js;
                }
                map.put("fileId", insertFile.getId());
                js = new JsonResult(Constants.STATUS_SUCCESS, "上传成功", map);
            }
        }catch (Exception ex){
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "上传异常");
        }
        return js;
    }

    /**
     * 多文件上传
     * @param files
     * @return
     */
    @RequestMapping(value = "/uploadFiles", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult uploadFiles(MultipartFile[] files){
        JsonResult js;
        try {
            List<Map> list = UploadFileUtil.upload(files);
            for (Map map : list){
                if(map.get("code").toString().equals("200")){
                    String fileName = map.get("fileName").toString();
                    String fileUrl = map.get("fileUrl").toString();
                    String fileSize = map.get("fileSize").toString();
                    File insertFile = new File();
                    insertFile.setFileName(fileName);
                    insertFile.setFileUrl(fileUrl);
                    insertFile.setFileSize(fileSize);
                    int i = fileService.insert(insertFile);
                    map.put("fileId", insertFile.getId());
                }
            }
            js = new JsonResult(Constants.STATUS_SUCCESS, "上传成功", list);
        }catch (Exception ex){
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "上传异常");
        }
        return js;
    }
}
